// pages/collect/collect.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    https:wx.getStorageSync('https'),
    banner:wx.getStorageSync('banner'),
    UserName:'',
    UserHead:'',
    openid:'',
    Flower_Count:0,
    Collect_Count:0,
    Collect_Aids:'',
    Albums:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var t = this
    this.getinfo(t)
  },


  getinfo(t){
    wx.login({
      success:function(res1){
        var code = res1.code;
        wx.request({
          url:'https://photo.tobeyoung.net/NewAlbums/index.php/Home/Interface/Get_Openid',
          data:{
            code:code
          },
          success:function(res2){
            wx.getUserInfo({
              success: function(res3) {
                t.setData({
                  UserName:res3.userInfo.nickName,
                  UserHead:res3.userInfo.avatarUrl,
                  openid:res2.data.openid
                })
                t.Get_User_Flower_Collect(t)
              }
            })
          }
        })
      }
    })
  },






  //获取用户的收藏和点赞信息
  Get_User_Flower_Collect(t){
    wx.request({
      url:t.data.https+'get_user_flower_collect',
      data:{
        openid:t.data.openid,
      },
      success:function(res){
        if(res.data.Collect_Aids!=null){
          var Collect_Count = res.data.Collect_Aids.length
          var Collect_Aids = res.data.Collect_Aids.join(',')
        }else{
          var Collect_Count = t.data.Collect_Aids.length
          var Collect_Aids = t.data.Collect_Aids
        }
        if(res.data.Flower_Aids!=null){
          var Flower_Count = res.data.Flower_Aids.length
          var Flower_Aids = res.data.Flower_Aids.join(',')
        }else{
          var Flower_Count = t.data.Flower_Count
          var Flower_Aids = t.data.Flower_Aids
        }
        wx.request({
          url:t.data.https+'Get_Collect_Albums',
          data:{
            aids:Collect_Aids
          },
          success:function(res){
            var Albums = res.data
            if(Flower_Aids){
              Flower_Aids = Flower_Aids.split(',')
              for(var j=0;j<Albums.length;j++){
                for(var i=0;i<Flower_Aids.length;i++){
                  if(Flower_Aids[i] == Albums[j].album_id){
                    Albums[j].album_flower_stat = 1
                  }
                }
              }
            }
            t.setData({
              Albums:res.data,
              Collect_Count:Collect_Count,
              Flower_Count:Flower_Count
            })
          }
        })
      }
    })
  },


  //获取收藏的相册信息
  Get_Collect_Albums(t){
    wx.request({
      url:t.data.https+'Get_Collect_Albums',
      data:{
        aids:t.data.Collect_Aids
      },
      success:function(res){
        var Albums = res.data
        t.setData({
          Albums:res.data
        })
      }
    })
  },


  //切换收藏状态
  Collect_Update(e){
    var aid = e.currentTarget.dataset.aid
    var openid = this.data.openid
    var t = this
    wx.request({
      url:t.data.https+'set_collect',
      data:{
        aid:aid,
        openid:openid
      },
      success:function(res){
        t.Get_User_Flower_Collect(t)
      }
    })
  },


  //切换点赞状态
  Flower_Update(e){
    var aid = e.currentTarget.dataset.aid
    var openid = this.data.openid
    var t = this
    wx.request({
      url:t.data.https+'set_flower',
      data:{
        aid:aid,
        openid:openid
      },
      success:function(res){
        t.Get_User_Flower_Collect(t)
      }
    })
  },
  



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})